package com.uchengwang.ui.cm_ui.uu.ui;

import android.os.Bundle;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.UUConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ActivityTitleBar;
import com.uchengwang.ui.base_ui.ui.AllBaseActivity;
import com.uchengwang.ui.cm_ui.uu.adapter.ChooseChatAdapter;
import com.uchengwang.ui.cm_ui.uu.bean.ChooseChatBean;
import com.uchengwang.ui.cm_ui.uu.bean.UChatBean;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.uchengwang.ui.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/6/13.
 * 选择聊天话题
 */

public class ChooseChatActivity extends AllBaseActivity implements View.OnClickListener{
    private LinearLayout woman_ln, man_ln;
    private AppCompatCheckBox woman_check, man_check;
    private MyGridView gv;
    private EditText edit;
    private Button bt;
    private TextView woman_text, man_text;
    private int sex = 0;//0女 1男

    private ChooseChatAdapter adapter;
    private List<ChooseChatBean> list;
    private String chooseS;//选择的话题
    private ActivityTitleBar titleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_choose_chat);

        
        initView();

    }

    private void initView() {
        titleBar = (ActivityTitleBar) findViewById(R.id.choose_titleBar);
        titleBar.setTitle("选择", 0);
        woman_ln = (LinearLayout) findViewById(R.id.choose_woman);
        woman_ln.setOnClickListener(this);
        man_ln = (LinearLayout) findViewById(R.id.choose_man);
        man_ln.setOnClickListener(this);
        woman_check = (AppCompatCheckBox) findViewById(R.id.choose_woman_check);
        man_check = (AppCompatCheckBox) findViewById(R.id.choose_man_check);
        woman_text = (TextView) findViewById(R.id.choose_woman_text);
        man_text = (TextView) findViewById(R.id.choose_man_text);

        gv = (MyGridView) findViewById(R.id.choose_gv);
        edit = (EditText) findViewById(R.id.choose_edit);
        bt = (Button) findViewById(R.id.choose_bt);
        bt.setOnClickListener(this);

//        initData();
    }

    private void initData(){
        list = new ArrayList<>();
        list.add(new ChooseChatBean(false, "单身"));
        list.add(new ChooseChatBean(false, "爱情"));
        list.add(new ChooseChatBean(false, "理想"));
        list.add(new ChooseChatBean(false, "人生"));
        list.add(new ChooseChatBean(false, "朋友"));
        list.add(new ChooseChatBean(false, "家庭"));
        list.add(new ChooseChatBean(false, "学业"));
        list.add(new ChooseChatBean(false, "闲聊"));

        adapter = new ChooseChatAdapter(this, list);

        gv.setAdapter(adapter);

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                for (ChooseChatBean bean : list){
                    bean.setCheck(false);
                }
                ChooseChatBean chatBean = list.get(i);
                chooseS = chatBean.getTitle();
                chatBean.setCheck(true);
                adapter.notifyDataSetChanged();

            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.choose_woman:
                woman_check.setChecked(true);
                man_check.setChecked(false);
                woman_text.setTextColor(getResources().getColor(R.color.ucw_color));
                man_text.setTextColor(getResources().getColor(R.color.ustudy_no_choose));
                sex = 0;
                break;
            case R.id.choose_man:
                woman_check.setChecked(false);
                man_check.setChecked(true);
                woman_text.setTextColor(getResources().getColor(R.color.ustudy_no_choose));
                man_text.setTextColor(getResources().getColor(R.color.ucw_color));
                sex = 1;
                break;
            case R.id.choose_bt:
//                if (TextUtils.isEmpty(chooseS)){
//                    UCommonUtil.getUtil().showToast(this, "请选择一个话题");
//                    return;
//
//                }
                LoadingUtils.getUtils().loading(this);
                //发布U聊
                MapRequest map = new MapRequest();
                map.put(BaseConfig.WANT_SEX, sex);
                map.put(BaseConfig.NICK_NAME, UChengApplication.nickName);
                map.put(BaseConfig.USER_AVATAR, UChengApplication.userAvatar);
//                map.put(BaseConfig.CHAT_TOPIC, chooseS);
                map.put(BaseConfig.USER_SIGN, edit.getText().toString());
                map.put(BaseConfig.EXPIRE_TIME, System.currentTimeMillis()+30*60*1000);//30分钟

                HttpManager.newInstance(this).query(UUConfig.GetTalks.method_create, UUConfig.GetTalks.URL, map, new HttpCallBack() {
                    @Override
                    public void onError(int status, String msg) {
                        Log.d("jjf", "stutas="+status+"=msg="+msg);
                        LoadingUtils.getUtils().hide();
                        UCommonUtil.getUtil().showToast(ChooseChatActivity.this, "发布失败");
                    }

                    @Override
                    public void onResp(int status, String msg, String data) {
                        Log.d("jjf", "stutas="+status+"=msg="+msg);
                        LoadingUtils.getUtils().hide();
                        if (status == 1){//今天已经发布过
                            if (!TextUtils.isEmpty(data)){
                                UChatBean bean = JsonUtils.getManager().stringToBean(data, UChatBean.class);
                                if (bean ==null){
                                    return;
                                }
//                                Intent intent = new Intent(ChooseChatActivity.this, UChatActivity.class);
////                                intent.putExtra(BaseConfig.TOPIC, bean.getChatTopic());
//                                intent.putExtra(BaseConfig.SEX, bean.getWantSex());
//                                startActivity(intent);
//
//                                ChooseChatActivity.this.finish();
                            }
                        }else if (status ==0){
                            //设置
//                            Intent intent = new Intent(ChooseChatActivity.this, UChatActivity.class);
////                            intent.putExtra(BaseConfig.TOPIC, chooseS);
//                            intent.putExtra(BaseConfig.SEX, sex);
//                            startActivity(intent);
//                            ChooseChatActivity.this.finish();
                        }else {
                            UCommonUtil.getUtil().showToast(ChooseChatActivity.this, "发布失败");
                        }
                    }
                });
                break;
        }
    }
}
